package com.liugan.magic.actor;

import org.loon.framework.android.game.core.geom.RectBox;

import com.liugan.magic.Constants;
import com.liugan.magic.actor.base.BaseActor;
import com.liugan.magic.map.Magicmap;
import com.liugan.magic.screen.PointXY;
import com.liugan.magic.screen.layer.GameLayer;
import com.liugan.magic.utils.AnimationUtils;
import com.liugan.magic.utils.TransFormer;

public class Illusionist extends BaseActor{
	
	public boolean istransform=false;
	private boolean jump, fail;
	private int previousx,previousy;//前一个复活点x
	
	//地图的高
	private int mapheight=32*Magicmap.TILE_SIZE;
	
	public Illusionist(PointXY totalXY,PointXY offsetXY) {
		setAnimation(true);
		ct.setCT("magic", 20, 10, 1, 4, -15,20,0);//设定属性
		this.setAnimation(AnimationUtils.getinstance().rightstandanimation);
		this.isdir="right";
		this.totalXY=totalXY;
		this.offsetXY=offsetXY;
		this.fail=false;
	}
	
	public void action(long elapsedTime) {
		if (layer==null){
			layer=(GameLayer) getLayer();
		}
		//跳起来以后y初始速度
		if (jump){
			vy=ct.ys;
			jump=false;
			onGround=false;
		}
		runbyarea();
		//改变动作形状
		changeshape();
		//检测是否失败
		checkforfail();
		//是否捡到了钥匙
		checkforkey();
		//是不是开门
		checkfordoor();
	}


	private void checkfordoor() {
		//如果拿到了钥匙并且 是人形碰到门
		if (!layer.getKey().isVisible()&&("magic").equals(ct.Actortype)&&isCollisionx(layer.getDoor())){
			layer.getDoor().setAnimation(AnimationUtils.getinstance().doora);
			setVisible(false);
		}
	}

	public boolean isfail(){
		return fail;
	}
	public void stop() {
		vx = 0;
	}
	public void stopfly() {
		vy = 0;
	}
	public void left() {
		vx = -ct.xs;
		isdir = "left";
	}

	public void right() {
		vx = ct.xs;;
		isdir = "right";
	}

	public void jump() {
		if (onGround) {
			jump = true;
		}
	}
	
	/** 检测失败
	 * 
	 */
	public void checkforfail(){
		if (this.totalXY.y>640){
			this.fail=true;
		}
	}
	
	/** 检测碰撞钥匙
	 * 
	 */
	private void checkforkey() {
		
		//碰到钥匙
		if (layer.getKey()!=null&&layer.getKey().isCollisionx(this)&&layer.getKey().isVisible()){
			layer.getKey().setVisible(false);
			
			//记录已经拿到钥匙
			
			//在右上角显示钥匙
			
		}
		
	}
	/**
	 * 相应状态
	 * 
	 */
	public void changeshape() {
		TransFormer.setState(this);
	}

	/**
	 *
	 *
	 */
	public void runbyarea(){
		
		//变形过程中 不计算行走
		if(this.istransform){
			return;
		}
		vy += ct.slowys;//减速
		newx=offsetXY.x+(int)vx;
		newy=offsetXY.y+(int)vy;
		//首先检测碰撞
		int layerWidth =getLayer().getWidth();
		int layerHeight=getLayer().getHeight();
		//如果太低了
		int beginy=totalXY.y-offsetXY.y;
		int beginx=totalXY.x-offsetXY.x;
		int layerbegin=mapheight-beginy;
		
		// @ TODO  检测必须要加预警区
		//根据高手的代码 分别来判断xy的碰撞
		for (int i=0;i<layer.getActorList().size();i++){
			//不能走出开始坐标
			if((beginx<=0)&&(vx<0)){
				if ((getX()>=layerWidth*Constants.movelimt_left&&getX()<=layerWidth*Constants.movelimt_right)||(getX()>layerWidth*Constants.movelimt_right&&vx<0)||(getX()<layerWidth*Constants.movelimt_left&&vx>0)){
					
				}else{
					vx = 0;
					break;
				}
			}
			//如果不属于x方向碰撞类型就不检测
			if((!getbyactortype_x(layer.getActorList().get(i)))&&this.isCollisionx(layer.getActorList().get(i))){
				vx = 0;
				//这里还需要一个设置 就是设置 碰撞点的坐标. 也就是虽然下一步碰撞了.但是在此时没有碰撞.
				//所以要将x设置为碰撞的最大值.也就是说是碰撞的边界. 根据宽度 和碰撞的actor的位置确定
				break;
			}
			//x没有碰撞
			if (i==(layer.getActorList().size()-1)){
				if ((getX()>=layerWidth*Constants.movelimt_left&&getX()<=layerWidth*Constants.movelimt_right)||(getX()>layerWidth*Constants.movelimt_right&&vx<0)||(getX()<layerWidth*Constants.movelimt_left&&vx>0)){
					setX(newx);
					offsetXY.x=newx;
					totalXY.x+=vx;
				}else {
					totalXY.x+=vx;
				}
			}
			//刷怪 = =
			if ( Math.abs(layer.getActorList().get(i).totalXY.getX()-this.totalXY.getX())>500&&!layer.getActorList().get(i).isVisible()){
				layer.getActorList().get(i).setVisible(true);
			}
		}
		
		if (vy>0){
		//检测y方向
		for (int i=0;i<layer.getActorList().size();i++){
			//下一位置碰撞
			RectBox newrb=new RectBox(newx+ct.rx,newy+ct.ry,getWidth()-ct.rwidth,getHeight()-ct.rheight);
			if((!getbyactortype_y(layer.getActorList().get(i)))&&newrb.intersects(layer.getActorList().get(i).getrectboxy())){
				BaseActor ba=layer.getActorList().get(i);
				if ((this.getY()+getHeight())<(ba.getY()+ba.getHeight())){//脚碰地
					//计算停止点
					if (!("butterfly").equals(this.ct.Actortype)){
						float temy=(ba.getY()+ba.ct.ry)-(getY()+getHeight());
						this.setY(offsetXY.y+=temy);
						totalXY.y+=temy;
					}else{
						
					}
					//同理 需要设置碰撞的边界  结合actor的 intrudey
					vy = 0;
					onGround=true;
					break;
				}
			}
			
			//没有碰撞
			if (i==(layer.getActorList().size()-1)){
					//当地图触底,y方向将不随视角移动
					if (layerbegin<=layerHeight){//当触底了以后 将不再移动y视角
						this.setY(newy);
						offsetXY.y=newy;
						totalXY.y+=vy;
					}else{
						//但是这里有个特殊的地方 刚开始和结束.或是到顶了和到底了 都是要特殊对待
						if(this.ct.Actortype.equals("butterfly")){
							if((layerbegin>layerHeight)||(layerbegin<=layerHeight&&getY()<100&&vy<0)||(layerbegin<layerHeight&&getY()>layerHeight-100&&vy>0)){
								totalXY.y+=vy;
							}else{
								this.setY(newy);
								offsetXY.y=newy;
								totalXY.y+=vy;
							}
						}else{
							float centerY=getY()+getHeight()/2;
							if ((centerY>=layerHeight*0.15&&centerY<=layerHeight*0.7)||(centerY>layerHeight*0.7&&vy<0)||(centerY<layerWidth*Constants.movelimt_left&&vy>0)){
								this.setY(newy);
								offsetXY.y=newy;
								totalXY.y+=vy;
							}else {
								totalXY.y+=vy;
							}
						}
					}
					onGround=false;
				}
			}
			int removelength=layerHeight-layerbegin;
			
			//绘画超过了地图边界
			if(removelength>0){
				totalXY.y-=removelength;
			}
		}else{
			//如果是蝴蝶
			if("butterfly".equals(this.ct.Actortype)){
				
				if((layerbegin>layerHeight)||(layerbegin<=layerHeight&&getY()<100&&vy<0)||(layerbegin<layerHeight&&getY()>layerHeight-100&&vy>0)){
					totalXY.y+=vy;
				}else{
					offsetXY.y=newy;
					setY(newy);
					totalXY.y+=vy;
				}
			}else{
				
				float centerY=getY()+getHeight()/2;
				if ((centerY>=layerHeight*0.15&&centerY<=layerHeight*0.7)||(centerY>layerHeight*0.7&&vy<0)||(centerY<layerWidth*Constants.movelimt_left&&vy>0)){
					offsetXY.y=newy;
					setY(newy);
					totalXY.y+=vy;
				}else {
					totalXY.y+=vy;
				}
			}
			
		}
	}

	public void rebegin(){
		
		PointXY newoffset=new PointXY(100,100);
		PointXY newtotle=new PointXY(100,100);
		setX(100);
		setY(100);
		this.offsetXY.setxy(newoffset);
		this.totalXY.setxy(newtotle);
		fail=false;
		onGround=false;
		vy = -5;
	}
	
	public void up() {
		
		if("butterfly".equals(this.ct.Actortype)){
			int beginy=totalXY.y-offsetXY.y;
			//绘画超过了地图边界
			if(beginy<0){
				vy=0;
				return;
			}
			vy=ct.ys;
		}
		
	}
	public void down() {
		if("butterfly".equals(this.ct.Actortype)){
			vy=-ct.ys;
		}
	}
	
	//根据actor类型忽略x方向碰撞检测
	public boolean getbyactortype_x(BaseActor a){
		if (("key,door,grass,stone,butterfly,rabbit,fox,frog").indexOf(a.ct.Actortype)!=-1){
			return true;
		}else{
			return false;
		}
	}
	
	//根据actor类型忽略y方向碰撞检测
	public boolean getbyactortype_y(BaseActor a){
		//魔术师的
		if (this.ct.Actortype.equals("magic")){
			if (("key,door,butterfly,rabbit,fox,frog").indexOf(a.ct.Actortype)!=-1){
				return true;
			}else{
				return false;
			}
		//蝴蝶的碰撞忽略
		}else if (this.ct.Actortype.equals("butterfly")){
			if (("key,door,butterfly,rabbit,fox,frog,stone").indexOf(a.ct.Actortype)!=-1){
				return true;
			}else{
				return false;
			}
		//兔子的碰撞忽略
		}else if("rabbit".equals(this.ct.Actortype)){
			if (("key,door,butterfly,rabbit,fox,frog").indexOf(a.ct.Actortype)!=-1){
				return true;
			}else{
				return false;
			}
		//青蛙的碰撞忽略
		}else if("frog".equals(this.ct.Actortype)){
			if (("key,door,butterfly,rabbit,fox,frog").indexOf(a.ct.Actortype)!=-1){
				return true;
			}else{
				return false;
			}
		}else if("fox".equals(this.ct.Actortype)){
			if (("key,door,butterfly,rabbit,fox,frog").indexOf(a.ct.Actortype)!=-1){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}

	}
	//返回可以变换的类型
	public boolean gettranstype(BaseActor a){
		if (("butterfly,rabbit,fox,frog").indexOf(a.ct.Actortype)!=-1){
			return true;
		}else{
			return false;
		}
	}
	//变回魔术师
	public void canceltrans() {
		if(!this.istransform){
			if (!"magic".equals(this.ct.Actortype)){
				TransFormer.transformerTomagic(this);
			}
		}
	}
	//变幻
	public void transform() {
		//变形过程中不变形 = = 拗口
		if(!this.istransform){
			for (int i=0;i<layer.getActorList().size();i++){
				if(this.isCollisionx(layer.getActorList().get(i))){
					if((gettranstype(layer.getActorList().get(i)))&&(layer.getActorList().get(i).isVisible()==true)&&("magic".equals(this.ct.Actortype))){
						TransFormer.transformerToother(this, layer.getActorList().get(i));
						break;
					}
				}
			}
		}
	}
	
	
	
	
	
}
